#include "compressorwidget.h"
#include "dm/dmdef.h"


CompressorWidget::CompressorWidget(const double &maxThreshold, QWidget *parent)
    : AWidget{parent}
{
    setMouseTracking(true);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    auto layout = new QGridLayout(this);
    thresholdLabel = new ALabel(tr(LABEL_THRESHOLD), this);
    attackLabel = new ALabel(tr(LABEL_ATTACK), this);
    decayLabel = new ALabel(tr(LABEL_DECAY), this);
    ratioLabel = new ALabel(tr(LABEL_RATIO), this);
    thresholdSpinBox = new ADoubleSpinBox(this);
    attackSpinBox = new ASpinBox(this);
    decaySpinBox = new ASpinBox(this);
    ratioSpinBox = new ASpinBox(this);
    ucs = new DynamicXUCS(DynamicX::Compressor, maxThreshold, this);

    layout->addWidget(thresholdLabel, 0, 0, Qt::AlignHCenter);
    layout->addWidget(attackLabel, 0, 1, Qt::AlignHCenter);
    layout->addWidget(decayLabel, 0, 2, Qt::AlignHCenter);
    layout->addWidget(ratioLabel, 0, 3, Qt::AlignHCenter);
    layout->addWidget(thresholdSpinBox, 1, 0, Qt::AlignHCenter);
    layout->addWidget(attackSpinBox, 1, 1, Qt::AlignHCenter);
    layout->addWidget(decaySpinBox, 1, 2, Qt::AlignHCenter);
    layout->addWidget(ratioSpinBox, 1, 3, Qt::AlignHCenter);
    layout->addWidget(ucs, 2, 0, 1, 4);
    
    thresholdSpinBox->setDecimals(DYNAMIC_X_THRESHOLD_DECIMAL);
    thresholdSpinBox->setSingleStep(DYNAMIC_X_THRESHOLD_STEP);
    
    thresholdSpinBox->setRange(DYNAMIC_X_THRESHOLD_MIN, maxThreshold);
    attackSpinBox->setRange(DYNAMIC_X_ATTACK_MIN, DYNAMIC_X_ATTACK_MAX);
    decaySpinBox->setRange(DYNAMIC_X_DECAY_MIN, DYNAMIC_X_DECAY_MAX);
    ratioSpinBox->setRange(DYNAMIC_X_RATIO_MIN, DYNAMIC_X_RATIO_MAX);

    thresholdSpinBox->setSuffix(tr(LABEL_DECIBEL_UINT));
    attackSpinBox->setSuffix(tr(LABEL_TIME_MS_UNIT));
    decaySpinBox->setSuffix(tr(LABEL_TIME_MS_UNIT));

    connect(thresholdSpinBox, QOverload<double>::of(&ADoubleSpinBox::valueChanged), ucs, &DynamicXUCS::setThreshold);
    connect(ratioSpinBox, QOverload<int>::of(&ASpinBox::valueChanged), ucs, &DynamicXUCS::setRatio);

    connect(ucs, &DynamicXUCS::thresholdChanged, thresholdSpinBox, &ADoubleSpinBox::setValue);
    connect(ucs, &DynamicXUCS::ratioChanged, ratioSpinBox, &ASpinBox::setValue);

    connect(thresholdSpinBox, QOverload<double>::of(&ADoubleSpinBox::valueChanged), this, &CompressorWidget::valueChanged);
    connect(attackSpinBox, QOverload<int>::of(&ASpinBox::valueChanged), this, &CompressorWidget::valueChanged);
    connect(decaySpinBox, QOverload<int>::of(&ASpinBox::valueChanged), this, &CompressorWidget::valueChanged);
    connect(ratioSpinBox, QOverload<int>::of(&ASpinBox::valueChanged), this, &CompressorWidget::valueChanged);

    set(Compressor());
}

Compressor CompressorWidget::getData() const
{
    Compressor data;
    data.threshold = thresholdSpinBox->value();
    data.attack = attackSpinBox->value();
    data.decay = decaySpinBox->value();
    data.ratio = ratioSpinBox->value();
    return data;
}

void CompressorWidget::set(const Compressor &data)
{
    blockSignals(true);
    thresholdSpinBox->setValue(data.threshold);
    attackSpinBox->setValue(data.attack);
    decaySpinBox->setValue(data.decay);
    ratioSpinBox->setValue(data.ratio);
    blockSignals(false);
}

void CompressorWidget::valueChanged()
{
    emit stateChanged(getData());
}

